package com.btobtob.a.biz.user.controller;

import java.util.ArrayList;
import java.util.List;

import com.btobtob.a.biz.user.dto.clientobject.Demo1CO;
import com.btobtob.a.biz.user.dto.command.Demo1Cmd;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.type.DateTime;
import io.dapr.client.DaprClient;
import io.dapr.client.domain.HttpExtension;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.SingleResponse;
import com.btobtob.a.biz.user.dto.clientobject.WeatherForecastCO;
import com.btobtob.a.biz.user.dto.command.WeatherForecastCmd;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@Tag(name = "/dapr/j", description = "/dapr/j")
@RequestMapping("/dapr/j")
@Validated
@ResponseBody
public class DaprJController {

    @Resource
    private DaprClient daprClient;

    @Resource
    private ObjectMapper objectMapper;

    @GetMapping(value = "/get1")
    public SingleResponse<String> get1() {
        byte[] resp = daprClient.invokeMethod("btobtob-a-j-user", "/receiver/get", null, HttpExtension.GET, byte[].class).block();
        assert resp != null;
        String co = new String(resp);
        return SingleResponse.of(co);
    }

    @PostMapping(value = "/post")
    public SingleResponse<Demo1CO> post(@RequestBody Demo1Cmd cmd) throws Exception {
        System.out.println(objectMapper.writeValueAsString(cmd));
        Demo1CO co = daprClient.invokeMethod("btobtob-a-j-user", "/receiver/post", cmd, HttpExtension.POST, Demo1CO.class).block();
        return SingleResponse.of(co);
    }

    @PostMapping(value = "/post1")
    public SingleResponse<WeatherForecastCO> post1(@RequestBody WeatherForecastCmd cmd) throws Exception {
        WeatherForecastCO co = daprClient.invokeMethod("btobtob-a-j-user", "/receiver/post1", cmd, HttpExtension.POST, WeatherForecastCO.class).block();
        return SingleResponse.of(co);
    }


    @PostMapping(value = "/post2")
    public MultiResponse<WeatherForecastCO> post2(@RequestBody List<WeatherForecastCmd> cmds) {

        var cos = daprClient.invokeMethod("btobtob-a-j-user", "/receiver/post2", cmds, HttpExtension.POST, List.class).block();
        return MultiResponse.of(cos);

//        List<WeatherForecastCO> cos = new ArrayList<>();
//        for (WeatherForecastCmd cmd : cmds) {
//            WeatherForecastCO co = new WeatherForecastCO();
////            co.setDate(cmd.getDate());
////            co.setDatetime(cmd.getDatetime());
//            co.setTemperatureC(cmd.getTemperatureC());
//            co.setSummary(cmd.getSummary());
//            cos.add(co);
//        }
//        return MultiResponse.of(cos);
    }

}
